package com.ruoyi.platform.system.message;

import com.ruoyi.base.controller.BaseController;
import com.ruoyi.base.pojo.HttpResult;
import com.ruoyi.base.redis.phone.PhoneCodeRedis;
import com.ruoyi.base.utils.inputcheck.InputCheckUtils;
import com.ruoyi.base.utils.message.MessageUtils;
import com.ruoyi.base.utils.number.NumberUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 短信
 * @author lcf
 */
@Api(tags = {"短信"})
@RestController
@RequestMapping("/message")
public class MessageController extends BaseController {
    @Autowired
    private PhoneCodeRedis phoneCodeRedis;

    /**
     * 发送短信验证码
     */
    @ApiOperation("发送短信验证码")
    @PostMapping
    public HttpResult<String> sendPhoneCode(@ApiParam(value = "手机号", required = true) String phone) {
        InputCheckUtils.checkStr(phone, "手机号码为空");

        //检查发送是否过于频繁
        phoneCodeRedis.checkTimeBetween(phone);
        //缓存4位数随机验证码
        String code = NumberUtils.randomInt(4);
        phoneCodeRedis.cache(phone, code);
        //发送短信
        MessageUtils.sendPhoneCode(phone, code);

        return success();
    }
}
